const Koa = require('koa')

const pageRouter = require('./routes/dev-ssr')

const app = new Koa()

const isDev = process.env.NODE_ENV === 'develoment'

// 中间件
app.use(async(ctx, next) => {
    try {
        console.info(`request with path ${ctx.path}`)
        await next()
    } catch (err) {
        console.info(err)
        ctx.status = 500
        if (isDev) {
            ctx.body = err.message
        } else {
            ctx.body = 'please try again !'
        }
    }
})

app.use(pageRouter.routes()).use(pageRouter.allowedMethods())

const HOST = process.env.HOST || '0.0.0.0'
const PORT = process.env.PORT || 3333

app.listen(PORT, HOST, () => {
    console.log(`server is listening on ${HOST}:${PORT}`)
})